package com.seguriapp.administration.gwt.client.activity;

import java.util.List;

import net.customware.gwt.dispatch.client.DispatchAsync;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.seguriapp.administration.gwt.client.place.EditUserDataPlace;
import com.seguriapp.administration.gwt.shared.GetUsersDataAction;
import com.seguriapp.administration.gwt.shared.GetUsersDataResult;
import com.seguriapp.common.gwt.client.AbstractSeguriAppActivity;
import com.seguriapp.common.gwt.client.AbstractSeguriAppPlace;
import com.seguriapp.common.gwt.client.SeguriAppCallback;
import com.seguriapp.common.gwt.client.SeguriAppClientSession;
import com.seguriapp.common.gwt.client.SeguriAppDisplay;
import com.seguriapp.common.gwt.client.pagination.PageChangedEvent;
import com.seguriapp.common.gwt.client.pagination.PageChangedEventHandler;
import com.seguriapp.common.gwt.client.pagination.SearchPagination;
import com.seguriapp.common.gwt.client.pagination.SearchPaginationPresenter;
import com.seguriapp.entities.User;

public class UsersListActivity extends AbstractSeguriAppActivity {

  private final Display display;
  private List<User> usersList;
  private long page;
  private boolean ascending;
  private String columnSort;

  public UsersListActivity(final DispatchAsync dispatchAsync, final EventBus eventBus, final PlaceController placeController, final Display display,
      final AbstractSeguriAppPlace place, final SeguriAppClientSession clientSession) {
    super(dispatchAsync, eventBus, placeController, place, clientSession);
    display.setPresenter(this);
    this.display = display;
  }

  public void setPage(final long page) {
    this.page = page;
  }

  public void setAscending(final boolean ascending) {
    this.ascending = ascending;
  }

  public void setColumnSort(final String columnSort) {
    this.columnSort = columnSort;
  }

  @Override
  protected void onStart(final AcceptsOneWidget widget, final EventBus eventBus) {
    widget.setWidget(display.asWidget());
    setPage(FIRST_PAGE);
    initializeListHandlers();
    updateUsersList();
  }

  private void initializeListHandlers() {
    new SearchPaginationPresenter(eventBus, display.getPagination());
    eventBus.addHandler(PageChangedEvent.TYPE, new PageChangedEventHandler() {

      public void onPageChanged(PageChangedEvent event) {
        setPage(event.getPage());
        updateUsersList();
      }
    });
  }

  public void updateUsersList() {
    final GetUsersDataAction action = new GetUsersDataAction();
    action.setPage(page);
    action.setAscending(ascending);
    action.setColumnSort(columnSort);
    dispatchAsync.execute(action, new SeguriAppCallback<GetUsersDataResult>() {

      public void onSuccess(final GetUsersDataResult result) {
        usersList = result.getUsersList();
        if (usersList != null && !usersList.isEmpty()) {
          display.setGridData(usersList);
          display.setPagination(result.getCurrentPage(), result.getTotalPages(), result.getMaxPages());
        } else {
          display.showNoDataMessage();
        }
      }

      public void onFailure(final Throwable e) {
        Window.alert("Error: " + e.getMessage());
      }
    });

  }

  public void goToEditUserPage(final User user) {
    clientSession.remove(SeguriAppClientSession.Key.User);
    clientSession.remove(SeguriAppClientSession.Key.isNewUser);
    if (user == null) {
      clientSession.put(SeguriAppClientSession.Key.isNewUser, true, true);
    } else {
      clientSession.put(SeguriAppClientSession.Key.User, user, true);
      clientSession.put(SeguriAppClientSession.Key.isNewUser, false, true);
    }
    final Place place = new EditUserDataPlace("");
    placeController.goTo(place);
  }

  public interface Display extends SeguriAppDisplay {

    void setPresenter(UsersListActivity presenter);

    void showNoDataMessage();

    void setPagination(long currentPage, long totalPages, long maxPages);

    void setGridData(List<User> usersList);

    SearchPagination getPagination();
  }

}
